/*
 * This is Melet, a salary slip designer. Feed it with data regarding to salary payment, prepare it for print through the graphical presentation, and you have it done.
 * Copyright (C) 2014, AFDiaX
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * This program is based on UMLet, a Free UML Tool for Fast UML Diagrams.
 * To contact the authors of UMLet visit https://code.google.com/p/umlet/people/list
 * To contact the authors of Melet visit https://gna.org/users/gabocze
 */
/**
 * 
 */
package com.baselet.gui.listener;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

import com.baselet.control.basics.geom.Point;
import com.baselet.control.basics.geom.Rectangle;
import com.baselet.control.enums.ElementId;
import com.baselet.element.ElementFactorySwing;
import com.baselet.element.interfaces.GridElement;
import com.baselet.gui.CurrentGui;

/**
 * @author Clara Yael Bradichansky
 *
 */
public class UpdateDrawPanelDocumentListener implements DocumentListener {
	Map<Object, Point> defaultLocations;
	Map<JComponent, GridElement> associations;
	private JSplitPane mainSplit;
	/**
	 * @param defaultLocations
	 */
	public UpdateDrawPanelDocumentListener(Map<Object, Point> defaultLocations, Map<JComponent, GridElement> associations) {
		super();
		this.defaultLocations = defaultLocations;
		this.associations = associations;
	}

	/* (non-Javadoc)
	 * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
	 */
	@Override
	public void changedUpdate(DocumentEvent e) {
		// Nothing to do here.
	}

	/* (non-Javadoc)
	 * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
	 */
	@Override
	public void insertUpdate(DocumentEvent e) {
		// Par�metro 1: updatedText es o no "" ?
		// Par�metro 2: :JComponent child de :DrawPanel conectado a e.getSource (existe o no?)

		try {
			Object key = e.getDocument().getProperty("source");
			GridElement element = associations.get(key);
			if(element==null) {
					Point location = defaultLocations.get(key);
					element = ElementFactorySwing.create(ElementId.Text, new Rectangle(location.x, location.y, 88, 96), "", null, CurrentGui.getInstance().getGui().getCurrentDiagram().getHandler());
			
					CurrentGui.getInstance().getGui().getCurrentDiagram().addElement(element);
					associations.put((JComponent)key, element);
			}
			element.setPanelAttributes(new StringBuilder("halign=left").append('\n')
					.append(e.getDocument().getText(0, e.getDocument().getLength())).append('\n')
	.append("layer=1").toString());

		} catch (BadLocationException e1) {
			throw new RuntimeException(e1);
		}
		//mainSplit.revalidate();

	}

	/* (non-Javadoc)
	 * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
	 */
	@Override
	public void removeUpdate(DocumentEvent e) {
		// Par�metro 1: updatedText es o no "" ?
		// Par�metro 2: :JComponent child de :DrawPanel conectado a e.getSource (existe o no?)
		Object key = e.getDocument().getProperty("source");
		final GridElement element = associations.get(key);
		try {
			element.setPanelAttributes(new StringBuilder("halign=left").append('\n')
					.append(e.getDocument().getText(0, e.getDocument().getLength())).append('\n')
.append("layer=1").toString());
		} catch (BadLocationException e1) {
			throw new RuntimeException(e1);
		}

		if(e.getDocument().getLength()==0) {
			associations.remove((JComponent)key);
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					CurrentGui.getInstance().getGui().getCurrentDiagram().removeElement(element);
				}
			});
		}
		//mainSplit.revalidate();
	}

	public void setComponent(JSplitPane mainSplit) {
		this.mainSplit = mainSplit;
		
	}
	
	
	
}